package fr.umlv.m2.jee.pmlv.service.subject.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import fr.umlv.m2.jee.pmlv.persistence.subject.KeyWord;
import fr.umlv.m2.jee.pmlv.persistence.subject.dao.IKeyWordDAO;
import fr.umlv.m2.jee.pmlv.service.subject.DisplayKeyWord;
import fr.umlv.m2.jee.pmlv.service.subject.IDisplayKeyWordService;
import java.util.Collections;
import java.util.Comparator;
import org.springframework.transaction.annotation.Transactional;

@Service("defautDisplayKeyWordService")
public class DefautDisplayKeyWordService implements IDisplayKeyWordService{

	@Autowired
	@Qualifier("hibernateKeyWordDAO")
	private IKeyWordDAO dao;

	@Override
        @Transactional(readOnly=true)
	public List<DisplayKeyWord> getAllDisplayKeyWord() {
            ArrayList< DisplayKeyWord > result = new ArrayList< DisplayKeyWord >(16);
            for(KeyWord keyWord : dao.findAll() ) {
                result.add(new DisplayKeyWord(keyWord));
            }
            Collections.sort(result, new Comparator<DisplayKeyWord>() {
                @Override
                public int compare(DisplayKeyWord o1, DisplayKeyWord o2) {
                    return o1.getWord().compareTo(o2.getWord());
                }
            });
            return result;
	}

}
